package utilities 
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.filters.GlowFilter;
	import flash.text.ime.CompositionAttributeRange;
	import org.flixel.*;
	
	/**
	 * ...
	 * @author Andrew Lau
	 */
	public class BoxHighlight extends FlxSprite
	{
		private var leftPoint:FlxPoint;
		private var rightPoint:FlxPoint;
		private var currentLeftPoint:FlxPoint;
		private var currentRightPoint:FlxPoint;
		
		private var delta:Array;
		
		private var thickness:uint;
		private var lineColor:uint;
		
		private var time:Number;
		
		private var glow:GlowFilter;
		
		private var _done:Boolean;
		
		private var callback:Function;
		
		public function BoxHighlight(topLeftX:Number, topLeftY:Number, botRightX:Number, botRightY:Number, color:uint = 0x0000ff21, thickness:uint = 1, seconds:Number = .75, glow:GlowFilter = null)
		{
			super(0, 0);
			makeGraphic(FlxG.width, FlxG.height, 0x00000000);
			
			var gfx:Graphics = FlxG.flashGfx;
			var gfxSprite:Sprite = FlxG.flashGfxSprite;		
			
			this.thickness = thickness;
			lineColor = color;
			
			time = seconds;
			
			this.glow = glow;
			
			_done = false;
			
			leftPoint = new FlxPoint(topLeftX, topLeftX);
			rightPoint = new FlxPoint(botRightX, botRightY);
			currentLeftPoint = new FlxPoint(0, 0);
			currentRightPoint = new FlxPoint(FlxG.width, FlxG.height);
			
			var updates:Number = (1 / FlxG.elapsed) * seconds;
			
			delta = new Array(topLeftX / updates, topLeftY / updates, (currentRightPoint.x - rightPoint.x) / updates, (currentRightPoint.y - rightPoint.y) / updates);

			if (glow == null) this.glow = new GlowFilter(0x0000ff21, 1, 8, 8, 6, 2);
		}
		
		override public function update():void
		{
			super.update();
			
			if(!_done) {
				if (time > 0) {
					
					this._pixels.fillRect(_pixels.rect, 0);
					
					time -= FlxG.elapsed;
					
					currentLeftPoint.x += delta[0];
					currentLeftPoint.y += delta[1];
					
					currentRightPoint.x -= delta[2];
					currentRightPoint.y -= delta[3];
					
					var gfx:Graphics = FlxG.flashGfx;
					var gfxSprite:Sprite = FlxG.flashGfxSprite;		
					
					gfx.clear();
					
					gfx.moveTo(currentLeftPoint.x, currentLeftPoint.y);
					
					var alphaComponent:Number = Number((lineColor >> 24) & 0xFF) / 255;
					if(alphaComponent <= 0)
						alphaComponent = 1;
					gfx.lineStyle(thickness, lineColor, alphaComponent);
					gfx.lineTo(currentRightPoint.x, currentLeftPoint.y);
					gfx.lineTo(currentRightPoint.x, currentRightPoint.y);
					gfx.lineTo(currentLeftPoint.x, currentRightPoint.y);
					gfx.lineTo(currentLeftPoint.x, currentLeftPoint.y);

					//gfxSprite.filters = [ this.glow ];
					
					_pixels.draw(gfxSprite);
					dirty = true;
				} else {
					_done = true;
					if(callback != null) callback(this);
				}
			}
		}
		
		public function addCallback(callback:Function = null):void
		{
			this.callback = callback;
		}
		
		public function get done():Boolean
		{
			return _done;
		}
		
	}

}